﻿namespace ExamV5.OnlineExam
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class ExamList : Page
    {
        protected Button butQuery;
        protected GridView DataGridInfo;
        protected DropDownList DDLQuery;
        protected DropDownList DDLSubject;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected Label labRescordCount;
        protected LinkButton LBFirstPage;
        protected LinkButton LBLastPage;
        protected LinkButton LBNextPage;
        protected LinkButton LBPrevPage;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected TextBox txtEndDate;
        protected TextBox txtQuery;
        protected TextBox txtStartDate;
        protected UpdatePanel UPPart;

        protected void butQuery_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void DataGridInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.DataGridInfo.PageIndex = e.NewPageIndex;
            this.method_0();
        }

        protected void DataGridInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex == -1)
            {
                this.DataGridInfo.Attributes.Add("oldValue", "#FFFFFF");
                this.DataGridInfo.Attributes.Add("singleValue", "#FFFFFF");
            }
            else
            {
                e.Row.Attributes.Add("onmouseover", "this.bgColor='#F6F5F5'");
                if ((e.Row.DataItemIndex % 2) != 0)
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('oldValue')");
                }
                else
                {
                    e.Row.Attributes.Add("bgcolor", "#FFFFFF");
                    e.Row.Attributes.Add("onmouseout", "this.bgColor=document.getElementById('DataGridInfo').getAttribute('singleValue')");
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text.Length > Convert.ToInt32(this.ViewState["SubStrLen"].ToString()))
                {
                    e.Row.Cells[0].Text = "<a title=" + e.Row.Cells[0].Text + ">" + e.Row.Cells[0].Text.Substring(0, Convert.ToInt32(this.ViewState["SubStrLen"].ToString())) + "…</a>";
                }
                if (e.Row.Cells[1].Text.Length > 10)
                {
                    e.Row.Cells[1].Text = "<a title=" + e.Row.Cells[1].Text + ">" + e.Row.Cells[1].Text.Substring(0, 10) + "…</a>";
                }
                Label label = (Label) e.Row.Cells[4].FindControl("labNoExam");
                Label label2 = (Label) e.Row.Cells[5].FindControl("labExaming");
                Label label3 = (Label) e.Row.Cells[6].FindControl("labExamed");
                Label label4 = (Label) e.Row.Cells[7].FindControl("labCountExam");
                label.Text = "<a href='FrmLookExamUser.aspx?PaperID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "&Flag=A'>" + this.method_1(this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString(), "A") + "人</a>";
                label2.Text = "<a href='FrmLookExamUser.aspx?PaperID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "&Flag=B'>" + this.method_1(this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString(), "B") + "人</a>";
                label3.Text = "<a href='FrmLookExamUser.aspx?PaperID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "&Flag=C'>" + this.method_1(this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString(), "C") + "人</a>";
                label4.Text = "<a href='FrmLookExamUser.aspx?PaperID=" + this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString() + "&Flag=D'>" + this.method_1(this.DataGridInfo.DataKeys[e.Row.RowIndex]["ID"].ToString(), "D") + "人</a>";
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (this.ViewState["SortType"].ToString() == " Desc")
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "ExamName":
                            e.Row.Cells[0].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "SubjectName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "PaperFen":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↑"));
                            return;

                        case "ExamTime":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↑"));
                            return;
                    }
                    e.Row.Cells[0].Controls.Add(new LiteralControl("↑"));
                }
                else
                {
                    switch (this.ViewState["SortFiled"].ToString())
                    {
                        case "ExamName":
                            e.Row.Cells[0].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "SubjectName":
                            e.Row.Cells[1].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "PaperFen":
                            e.Row.Cells[2].Controls.Add(new LiteralControl("↓"));
                            return;

                        case "ExamTime":
                            e.Row.Cells[3].Controls.Add(new LiteralControl("↓"));
                            return;
                    }
                    e.Row.Cells[0].Controls.Add(new LiteralControl("↓"));
                }
            }
        }

        protected void DataGridInfo_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;
            if (this.ViewState["SortFiled"].ToString() == sortExpression)
            {
                if (!(this.ViewState["SortType"].ToString() == " Desc"))
                {
                    this.ViewState["SortType"] = " Desc";
                }
                else
                {
                    this.ViewState["SortType"] = " ASC";
                }
            }
            else
            {
                this.ViewState["SortFiled"] = e.SortExpression;
            }
            this.method_0();
        }

        protected void DDLSubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBFirstPage_Click(object sender, EventArgs e)
        {
            this.DataGridInfo.PageIndex = 0;
            this.method_0();
        }

        protected void LBLastPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageCount > 0)
            {
                this.DataGridInfo.PageIndex = this.DataGridInfo.PageCount - 1;
                this.method_0();
            }
        }

        protected void LBNextPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex < (this.DataGridInfo.PageCount - 1))
            {
                this.DataGridInfo.PageIndex++;
                this.method_0();
            }
        }

        protected void LBPrevPage_Click(object sender, EventArgs e)
        {
            if (this.DataGridInfo.PageIndex > 0)
            {
                this.DataGridInfo.PageIndex--;
                this.method_0();
            }
        }

        private void method_0()
        {
            string selectCommandText = "";
            selectCommandText = "select PaperStartTime,PaperEndTime,ID,ExamName,SubjectName,PaperFen,InputMan,ExamTime from PaperInfo  where  ID in (select DISTINCT PaperID from ExamUser) ";
            if (this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='IsLookTestExam'", "ParmValue").Trim() == "Y")
            {
                selectCommandText = selectCommandText + " and  (PaperStatus='A' or PaperStatus='C') ";
            }
            else
            {
                selectCommandText = selectCommandText + " and  PaperStatus='A' ";
            }
            if (this.ViewState["ManagExam"].ToString() != "Y")
            {
                if (!(this.ViewState["AddExam"].ToString() == "Y"))
                {
                    selectCommandText = selectCommandText + " and ID=0 ";
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BPower", "alert('考试监考权限未开放！')", true);
                }
                else
                {
                    selectCommandText = selectCommandText + " and InputUserCode='" + this.httpCookie_0.Value + "'";
                }
            }
            else
            {
                selectCommandText = selectCommandText + " and ID<>0 ";
            }
            if (this.DDLSubject.SelectedItem.Value != "0")
            {
                selectCommandText = selectCommandText + " and SubjectName like '%" + this.publicFun_0.CheckString(this.DDLSubject.SelectedItem.Text.Replace("│", "").Replace("├", "")) + "%'";
            }
            if (this.txtQuery.Text.Trim() != "")
            {
                selectCommandText = selectCommandText + " and " + this.publicFun_0.CheckString(this.DDLQuery.SelectedItem.Value) + " like '%" + this.publicFun_0.CheckString(this.txtQuery.Text) + "%'";
            }
            if (!((this.txtStartDate.Text != "") ? !(this.txtEndDate.Text != "") : true))
            {
                selectCommandText = selectCommandText + " and (InputTime>=Convert(DateTime,'" + this.publicFun_0.CheckString(this.txtStartDate.Text) + "') and InputTime<=Convert(DateTime,'" + this.publicFun_0.CheckString(this.txtEndDate.Text) + "'))";
            }
            selectCommandText = selectCommandText + " order by " + this.ViewState["SortFiled"].ToString() + this.ViewState["SortType"].ToString();
            SqlConnection selectConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnection);
            DataSet dataSet = new DataSet();
            adapter.Fill(dataSet, "DataInfo");
            this.DataGridInfo.DataSource = dataSet.Tables["DataInfo"].DefaultView;
            this.DataGridInfo.DataBind();
            this.labRescordCount.Text = "总共&nbsp;<font color='#0000C0'><b>" + dataSet.Tables["DataInfo"].Rows.Count.ToString() + "</b></font>&nbsp;条记录&nbsp;<font color='#0000C0'><b>" + Convert.ToString((int) (this.DataGridInfo.PageIndex + 1)) + "/" + this.DataGridInfo.PageCount.ToString() + "</b></font>&nbsp;页&nbsp;";
            adapter.Dispose();
            selectConnection.Close();
            selectConnection.Dispose();
        }

        private string method_1(string string_0, string string_1)
        {
            string values = "0";
            if (string_1 != "C")
            {
                if (string_1 == "D")
                {
                    values = this.publicFun_0.GetValues("select Count(*) as CountUser from ExamUser where PaperID=" + string_0, "CountUser");
                }
                else
                {
                    values = this.publicFun_0.GetValues("select Count(*) as CountUser from ExamUser where Flag='" + string_1 + "' and PaperID=" + string_0, "CountUser");
                }
            }
            else
            {
                values = this.publicFun_0.GetValues("select Count(*) as CountUser from ExamUser where (Flag='C' or Flag='D' or Flag='E') and PaperID=" + string_0, "CountUser");
            }
            if (values.Trim() == "")
            {
                values = "0";
            }
            return values;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) && (this.httpCookie_0.Value != ""))
            {
                base.Response.Expires = 0;
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() == "Yes")
                    {
                        this.txtQuery.Attributes.Add("onkeydown", "SubmitKeyClick('butQuery');");
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddExam,ManagExam  from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AddExam"] = reader["AddExam"].ToString();
                            this.ViewState["ManagExam"] = reader["ManagExam"].ToString();
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        this.ViewState["SortFiled"] = "ID";
                        this.ViewState["SortType"] = " Desc";
                        this.publicFun_0.LoadSubjectInfo(this.DDLSubject, this.httpCookie_0.Value, "Y", "B", this.httpCookie_3.Value);
                        this.DDLSubject.Items.Insert(0, new ListItem("--所有科目信息--", "0"));
                        string str = this.publicFun_0.GetValues("select ParmValue from SysParmInfo where ParmMemo='SubStrLength'", "ParmValue").Trim();
                        if (str.Trim() == "")
                        {
                            str = "20";
                        }
                        this.ViewState["SubStrLen"] = str;
                        this.method_0();
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                    }
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

